【进制转换】十进制转其他进制 _CDTemplate

CodeHunt里有个进制转换的36进制转换,以前在清澄做过,但是可惜没放在CSDN过,这个着实是暴力的进制转换没有错,但是为了以后的速度,还是在这里存一份好了……

顺便提一句,特别地,对十六进制的转换时:http://blog.csdn.net/okcd00/article/details/45196255 


对于任何一个数字,及10-36间的进制基数,接口 TBase(n,m)

【n】 long long 长度的原十进制整数

【m】 进制基数Base

Code:

#include <cmath> 
#include <cctype>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))

bool cmp(const int a, const int b)
{
	return a > b;
}

string TBase(ll in,int m)
{
	string s="";
	ll n=in;
	while(n)
	{
		ll mod=n%m;
		if(mod) s+= (mod<10)? '0'+mod : 'A'+mod-1 ;
		n/=m;
		if(!n) break;
	}
	return s;
}

int main()
{
	ll n;	int m;
	cin>>n>>m;
	cout<<"Trans "<< n <<" to Base-"<< m <<" :"<<TBase(n,m); 
	return 0;
}



几年前清澄上吾辈的C语言代码,怀念~ ~ 贴一下:

  1. #include<stdio.h>
  2. #include<string.h>
  3. int str[10000];
  4. int main()
  5. {
  6.     int n,m,len=1;
  7.     scanf("%d %d",&n,&m);
  8.     if(!n) {
  9.     printf("0\n");
  10.     return 0;}
  11.     while(n)
  12.     {
  13.         int mod=n%m;
  14.         str[len++]=mod;
  15.         n=(n-mod)/m;
  16.         if(!n) break;
  17.     }
  18.     while(--len)
  19.     {
  20.         if(str[len]<10) printf("%d",str[len]);
  21.         else printf("%c",str[len]-10+'A');
  22.     }
  23. }





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糖果天王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值